const fs = require('fs')

/*
*
* */
let addMapping = (mapping, router) => {
    for(let url in mapping){
        if(url.startsWith('GET ')){
            let path = url.substring(4)
            router.get(path, mapping[url])
        }else if(url.startsWith('POST ')){
            let path = url.substring(4)
            router.post(path, mapping[url])
        }else{
            console.log('error in the url')
        }
    }
}

/*
*
* */
module.exports.addControllers = (router, controllerDir = __dirname + '/controller')=>{
    let controllers = fs.readdirSync(controllerDir)
    controllers.filter((controller)=>{
        return controller.endsWith('.js')
    })

    controllers.map(controller=>{
        let mapping = require(controllerDir + '/' + controller)
        addMapping(mapping, router)
    })
}



